package com.aivin.lib_readnotes.activity;

import android.app.Activity;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.aivin.lib_base.tool.RouteUtils;
import com.aivin.lib_widget.TextViewVertical;
import com.aivin.lib_readnotes.R;
import com.aivin.lib_readnotes.adapter.ChapterAdapter;
import com.aivin.lib_widget.bean.MarginDecoration;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.gyf.immersionbar.ImmersionBar;
import com.wk.bmoblib.bean.Paper;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


/**
 * 一本书的所有章节展示
 */
@Route(path = RouteUtils.ChapterActivity)
public class ChapterActivity extends Activity {
    private TextViewVertical infoTv ;
    private String bookName="null" ;
    private Activity activity ;
    private RecyclerView recyclerView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor( R.color.color_2e4a7e)
                .init();

        activity= this ;
        int bookIndex = getIntent().getIntExtra("bookIndex" , 0);
        bookName = getIntent().getStringExtra("bookName"  );
        initView();
        getChapters(bookIndex) ;
    }


    public void getChapters( int bookIndex){
        BmobQuery<Paper> bmobQuery = new BmobQuery<>();
        bmobQuery.addQueryKeys("objectId,bookIndex,chapterIndex,chapterName,readedCount");
        bmobQuery.addWhereEqualTo("bookIndex", bookIndex);
        bmobQuery.setLimit(5000) ;
        bmobQuery.order("chapterIndex") ;

        bmobQuery.findObjects(new FindListener<Paper>() {
            @Override
            public void done(List<Paper> data, BmobException e) {
                if(data != null && data.size()>0 )   {

                    bookName= bookName.replace("(" ,"︵") ;
                    bookName= bookName.replace(")" ,"︶") ;
                    infoTv.setText(  "︵"+  bookName +"︶");
                    //注意：这里的 对象中只有指定列的数据。
                    updateChapters(data) ;
                }else{
                    infoTv.setText(getString(R.string.str_loadfailed));
                }
            }
        });
    }

    private void updateChapters(List<Paper> apps){
        recyclerView.setAdapter(new ChapterAdapter(  apps,  (obj, itemIndex) -> {
            if(obj instanceof  Paper){
                toGetPaper((Paper) obj);
            }
        }
        ));
    }

    private void toGetPaper(Paper paper  ){
        String jsonStr = JSON.toJSONString(paper) ;
        /*Intent intent = new Intent(this , PaperActivity.class);
        intent.putExtra("jsonStr" ,jsonStr) ;
        this.startActivity(intent);*/
        ARouter.getInstance().build(RouteUtils.PaperActivity)
                .withString("jsonStr", jsonStr)
                .navigation();
    }

    private void initView(){
        infoTv = findViewById(R.id.infoTv) ;
        infoTv.setText(getString(R.string.str_isloading));
        recyclerView =  findViewById(R.id.recycle);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.addItemDecoration(new MarginDecoration(this));

        SwipeRefreshLayout swipeRefreshLay = findViewById(R.id.swipeRefreshLay);
        swipeRefreshLay.setEnabled(false);

        findViewById(R.id.exitActivityBtn).setOnClickListener( v -> activity.finish() );
    }

}
